package cn.com.sinosoft.ares.insurance.service.exception.handler;

import cn.com.sinosoft.ares.common.result.Result;
import cn.com.sinosoft.ares.insurance.service.exception.ProductNotExistException;
import cn.com.sinosoft.ares.insurance.service.exception.RepetitionException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Description: 异常处理
 * Author: lishangmin
 * Created: 2018-06-25 09:37
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class InsuranceExceptionHandler {

    /**
     * 其他异常
     */
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(Exception e){
        log.error("Exception:",e);
        return Result.error();
    }

    /**
     * 自定义异常
     */
    @ExceptionHandler(value = {ProductNotExistException.class})
    public Result insuranceExceptionHandler(Exception e){
        log.warn("InsuranceException:",e);
        ProductNotExistException ex = (ProductNotExistException) e;
        return StringUtils.isEmpty(ex.getMessage())?Result.error() : Result.error(ex.getMessage());
    }

    @ExceptionHandler(value = {RepetitionException.class})
    public Result repetitionExceptionHandler(Exception e){
        log.warn("InsuranceException:",e);
        RepetitionException ex = (RepetitionException) e;
        return StringUtils.isEmpty(ex.getMessage())?Result.error() : Result.error(ex.getMessage());
    }

}
